// #include<bits/stdc++.h>
#include<iostream>
#include<algorithm>
using namespace std;

//素数筛选的方法
/*
#include <vector>
void sieve(int n) {
    vector<bool> is_prime(n + 1, true);  // 初始化
    is_prime[0] = is_prime[1] = false;  // 0 和 1 不是素数

    for (int i = 2; i * i <= n; i++) {  // 枚举到 sqrt(n)
        if (is_prime[i]) {  // 如果 i 是素数
            for (int j = i * i; j <= n; j += i) {
                is_prime[j] = false;  // 将 i 的倍数标记为非素数
            }
        }
    }

    // 输出所有素数
    for (int i = 2; i <= n; i++) {
        if (is_prime[i]) {
            cout << i << " ";
        }
    }
}

int main() {
    int n;
    cin >> n;  // 输入 n
    sieve(n);  // 筛素数
    return 0;
}
*/

//二分的模板
/*
#include <iostream>

using namespace std;

const int N = 1e5 + 10;

int n, q;
int a[N];

int main()
{
    cin >> n >> q;

    for(int i = 1; i <= n; i ++) cin >> a[i];

    while(q --)
    {
        int t, l, r, x;
        cin >> t >> l >> r >> x;
        if(t == 1)
        {
            while(l < r)
            {
                int mid = l + r >> 1;
                if(a[mid] >= x) r = mid;
                else l = mid + 1;
            }
            if(a[l] != x) cout << -1 << endl;
            else cout << l << endl;
        }
        else if(t == 2)
        {
            while(l < r)
            {
                int mid = l + r + 1 >> 1;
                if(a[mid] <= x) l = mid;
                else r = mid - 1;
            }
            if(a[l] != x) cout << -1 << endl;
            else cout << l << endl;
        }
        else if(t == 3)
        {
            while(l < r)
            {
                int mid = l + r >> 1;
                if(a[mid] >= x) r = mid;
                else l = mid + 1;
            }
            if(a[l] < x) cout << -1 << endl;
            else cout << l << endl;
        }
        else
        {
            while(l < r)
            {
                int mid = l + r >> 1;
                if(a[mid] > x) r = mid;
                else l = mid + 1;
            }
            if(a[l] <= x) cout << -1 << endl;
            else cout << l << endl;
        }
    }
    return 0;
}

*/


//整数查找
/*
const int N = 1e5 + 10;
int n, q;
int a[N];

// 查询处理函数
void solve(int solu, int l, int r, int x) {
    // 根据查询类型分别处理
    if (solu == 1) {
        int _found = lower_bound(a + l, a + r + 1, x) - a; // 使用 lower_bound 查找
        if (_found <= r && a[_found] == x) {
            cout << _found << endl;
        }
        else {
            cout << -1 << endl;
        }
    }
    else if (solu == 2) {
        int _found = upper_bound(a + l, a + r + 1, x) - a - 1; // 使用 upper_bound 查找
        if (_found >= l && a[_found] == x) {
            cout << _found << endl;
        }
        else {
            cout << -1 << endl;
        }
    }
    else if (solu == 3) {
        int _found = lower_bound(a + l, a + r + 1, x) - a;
        if (_found <= r) {
            cout << _found << endl;
        }
        else {
            cout << -1 << endl;
        }
    }
    else if (solu == 4) {
        int _found = upper_bound(a + l, a + r + 1, x) - a;
        if (_found <= r) {
            cout << _found << endl;
        }
        else {
            cout << -1 << endl;
        }
    }
}

int main() {
    // 输入数组长度和查询数量
    cin >> n >> q;
    for (int i = 1; i <= n; ++i) {
        cin >> a[i]; // 输入数组元素
    }
    while (q--) {
        int solu, l, r, x;
        cin >> solu >> l >> r >> x;
        solve(solu, l, r, x); // 处理查询
    }
    return 0;
}
*/


//最大通过数
/*
#include<bits/stdc++.h>
using namespace std;
#define int long long
int n, m, k;
const int N = 2e5 + 10;
int a[N], b[N];
int _count;

int solve() {
    int ans = 0;            //返回的关卡值
    for (int i = 0; i <= n; ++i) {
        int temp = k - a[i];
        if (temp < 0)
            break;
        int _found = upper_bound(b, b + 1 + m, temp) - b - 1 + i;
        ans = max(ans, _found);
    }
    return ans;
}


signed main()
{
    cin >> n >> m >> k;

    for (int i = 1; i <= n; ++i) {
        cin >> a[i];
        a[i] = a[i - 1] + a[i];
    }
    for (int i = 1; i <= m; ++i) {
        cin >> b[i];
        b[i] = b[i - 1] + b[i];
    }
    int ret = solve();
    cout << ret;
    return 0;
}
*/


//二分答案 
/*
const int N = 1e5+10;

int a[N];
int n;
ll k;

bool check(ll m){
    ll temp =k;
     for(int j=1;j<=n;++j){
         if(a[j]<m){
            temp = temp - (m-a[j]);
        }
        if(temp < 0)
            return false; //在0的下边  < 0
    }
    return true;		//在0的上边  >=0
}


void solve(){
    scanf("%d%lld",&n,&k);
    for(int i=1;i<=n;++i){
        scanf("%d",&a[i]);
    }
    ll l = 1;
    ll r = (ll)1e14;
    while(l < r){
        ll mid = l + r +1 >> 1;
        if(check(mid))
            l = mid;
        else
            r = mid-1;
    }
    printf("%lld\n",l);

}

int main() {

    solve();
    return 0;
}

*/


//整数查找

const int N = 1e5 + 10;
int n, q;
int a[N];

// 查询处理函数
void solve(int solu, int l, int r, int x) {
    // 根据查询类型分别处理
    if (solu == 1) {
       while( l< r) {
         int mid = l+r >>1;
         if(a[mid] >= x)
            r = mid;
         else {
            l = mid+1;
         }
       }
       if(a[l] != x)
            cout<<-1<<endl;
       else
            cout<<l<<endl;
    }
    else if (solu == 2) {
       while( l < r) {
          int mid = l+r+1 >>1;
          if(a[mid] <= x)
              l = mid;
          else {
            r = mid-1;
         }
       }
       if(a[l] != x)
            cout<<-1<<endl;
       else
            cout<<l<<endl;

    }
    else if (solu == 3) {
        while( l < r) {
         int mid = l+r >>1;
         if(a[mid] >= x)
            r = mid;
         else {
            l= mid+1;
         }
       }
       if(a[l] < x)
            cout<<-1<<endl;
       else
            cout<<l<<endl;
    }
    else if (solu == 4) {
        while( l < r) {
         int mid = l+r >>1;
         if(a[mid] > x)
            r = mid;
         else {
            l= mid+1;
         }
       }
       if(a[l] <= x)
            cout<<-1<<endl;
       else
            cout<<l<<endl;
    }
}

int main() {
    // 输入数组长度和查询数量

    cin >> n >> q;
    for (int i = 1; i <= n; ++i) {
        cin >> a[i]; // 输入数组元素
    }
    while (q--) {
        int solu, l, r, x;
        cin >> solu >> l >> r >> x;
        solve(solu, l, r, x); // 处理查询
    }
    return 0;
}
